package com.mbc.common.manager;

import java.sql.SQLException;
import java.util.List;

import com.mbc.common.data.CountryDataList;
import com.mbc.common.entities.Country;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.param.CriteriaParams;

public class CountryMgr extends AbstractMgr {

	public CountryMgr() {
	}

	public CountryMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(Country entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			entityManager.persist(entity);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			if(DataDuplicationException.isUniqueKyeError(e))
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}

	public void update(Country entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			entityManager.merge(entity);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete(String key) throws Exception {
		openConnection();
		try {
			beginTransaction();
			Country l_country = entityManager.find(Country.class, key);
			entityManager.remove(l_country);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			if(e.getCause().getCause().getCause() instanceof SQLException){
				if(ForeignConflictException.FOREIGN_CONFLICT_ERROR_CODE == ((SQLException)e.getCause().getCause().getCause()).getErrorCode())
					throw new ForeignConflictException(e);
				else
					throw e;
			}
			else
				throw e;
		} finally {
			closeConnection();
		}
	}

	public Country find(String key) throws Exception {
		openConnection();
		try {
			return entityManager.find(Country.class, key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public List<Country> findAll() throws Exception {
		openConnection();
		try {
			return entityManager.createNamedQuery("Country.findAll", Country.class).getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public CountryDataList findCountry(CriteriaParams cri) throws Exception {
		openConnection();
		try {
            String l_cri = " where c.countryCode like '%" + cri.getKeyword() + "%' or c.name like '%" + cri.getKeyword() + "%'";
            
            String l_searchQuery = "select c from Country c" + l_cri;
            
            List<Country> l_CountryList = entityManager.createQuery(l_searchQuery, Country.class)
                    .setFirstResult(cri.getIndex())
                    .setMaxResults(cri.getSize())
                    .getResultList();
            
            CountryDataList l_dataList = new CountryDataList();
            l_dataList.setEntityList(l_CountryList);
            
            if(cri.getIndex() == 0){
            	String l_countQuery = "select count(c.countryCode) from Country c" + l_cri;
            	l_dataList.setTotalRecord(entityManager.createQuery(l_countQuery, Long.class).getSingleResult());
            }
            
			return l_dataList;
		} catch (Exception e) {
			throw e;
		} finally{
			closeConnection();
		}
	}

//	public JasperPrint prepareCountryReport(CountryRpt info) throws Exception {
//		openConnection();
//		JasperPrint l_jsPrint = null;
//		try {
//			countryDAO = CommonDAOFactory.getCountryDAO(dbManager
//					.getConnection());
//
//			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(
//					countryDAO.prepareCountryReport(info));
//
//			JasperReport l_jsRpt = (JasperReport) JRLoader
//					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
//							+ "reports/CountryRpt.jasper");
//			HashMap<String, Object> l_params = new HashMap<String, Object>();
//			l_params.put("company", "MBC");
//			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);
//
//		} catch (JRException je) {
//			throw je;
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			closeConnection();
//		}
//
//		return l_jsPrint;
//
//	}
}
